/**
 * 用户服务
 * @TransactionAnnotation表示开启事务
 */
import TransactionAnnotation from '../../package/TransactionAnnotation'

export default class RoleBindingPermissionService {
    /**
     * 注册用户
     * @param table                 指定表名称
     * @param user                  用户属性
     * @returns {Promise<*>|void}
     *
     */

    addRolePermissionEntity(entity) {
        return this['session'].save('t_role_permission', entity);
    }

    /**
     * 批量绑定
     * @param entitys
     * @returns {Promise<unknown>}
     */
    @TransactionAnnotation
    addBatchRolePermissionEntity(entitys) {
        return new Promise(async (resolve, reject) => {
            try {
                if (!Array.isArray(entitys)) {
                    throw ({message: '批量录入的Entity不是数组!'})
                }
                const array = [];
                for (let i = 0; i < entitys.length; i++) {
                    const obj = await this['session'].save('t_role_permission',entitys[i]);
                    array.push(obj);
                }
                resolve(array);
            } catch (e) {
                reject(e);
            }
        })
    }

    /**
     * 保存
     * @param table
     * @param userName
     * @returns {*}
     */
    removeRolePermissionEntity(entity) {
        const sql = "delete from t_role_permission where t_rid = ? and t_pid = ?";
        return this['session'].executeSQL(sql,[entity.t_rid,entity.t_pid]);
    }

    /**
     * 批量删除
     * @param entitys
     * @returns {Promise<unknown>}
     */
    @TransactionAnnotation
    removeBatchRolePermissionEntity(entitys) {
        return new Promise(async (resolve, reject) => {
            try {
                if (!Array.isArray(entitys)) {
                    throw ({message: '批量录入的Entity不是数组!'})
                }
                const array = [];
                for (let i = 0; i < entitys.length; i++) {
                    const sql = "delete from t_role_permission where t_rid = ? and t_pid = ?";
                    const obj = await this['session'].executeSQL(sql,[entitys[i].t_rid,entitys[i].t_pid]);
                    array.push(obj);
                }
                resolve(array);
            } catch (e) {
                reject(e);
            }

        })
    }


    /**
     * 查询
     * @param table
     * @param start
     * @param size
     * @returns {*}
     */
    listRolePermissionEntities(rid) {
           //return this['session'].executeSQL("SELECT t_permission.t_id,t_permission.t_permName,t_permission.`t_classify`,b.`t_rid` FROM t_permission LEFT JOIN (SELECT * FROM  t_role_permission WHERE t_role_permission.`t_rid` = ?) AS b ON t_permission.t_id = b.`t_pid`",  [rid] );
        return this['session'].executeSQL("SELECT c.*,t_dictionary_detail.`t_info` FROM  (SELECT t_permission.t_id,t_permission.t_permName,t_permission.`t_classify`,b.`t_rid` FROM t_permission LEFT JOIN (SELECT * FROM  t_role_permission WHERE t_role_permission.`t_rid` = ? ) AS b ON t_permission.t_id = b.`t_pid`) AS c LEFT JOIN t_dictionary_detail ON c.t_classify =t_dictionary_detail.`t_id`",  [rid] );

    }


}


